package com.group11.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group11.common.dto.RegUser;
import com.group11.user.dto.UserInfo;
import com.group11.user.entity.Score;
import com.group11.user.entity.UserEnterprise;
import com.group11.user.entity.UserIn;
import com.group11.user.exception.AccountException;
import com.group11.user.mapper.ScoreMapper;
import com.group11.user.mapper.UserEnterpriseMapper;
import com.group11.user.mapper.UserInMapper;
import com.group11.user.mapper.UserMapper;
import com.group11.user.entity.User;
import com.group11.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatterBuilder;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private UserInMapper userInMapper;

    @Autowired
    private UserEnterpriseMapper userEnterpriseMapper;

    @Transactional
    @Override
    public void reg(String account, String password) {
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setRegTime(LocalDateTime.now());
        user.setStatus("y");
        user.setScore(0);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, account);
        if(userMapper.exists(queryWrapper)){
            //这里抛出异常，Controller层会捕获并返回错误信息
            throw new AccountException("账号"+account+"已存在");
        }
        userMapper.insert(user);
        QueryWrapper<User> account1 = new QueryWrapper<User>().eq("account", account);
        User user1 = userMapper.selectOne(account1);
        UserIn userIn = new UserIn(user1.getId(), "", "", "1970-1-1", "");
        userInMapper.insert(userIn);

    }

    @Transactional
    @Override
    public RegUser login(RegUser regUser) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, regUser.getAccount());
        queryWrapper.eq(User::getPassword, regUser.getPassword());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return null;
        }
        //为用户增加5个计分
        userMapper.updateScore(user.getId(), 5);
        //积分变动记录表增加记录的明细
        Score score = new Score();
        score.setUserId(user.getId());
        score.setReason("成功登录");
        score.setRecordTime(LocalDateTime.now());
        score.setDelta(5);
        scoreMapper.insert(score);
        RegUser result = new RegUser();
        result.setId(user.getId());
        result.setAccount(user.getAccount());
        result.setPassword(user.getPassword());
        result.setType(user.getType());
          return result;
    }

    @Override
    public User getById(int id){
        return userMapper.selectById(id);
    }

    @Override
    public void setUserType(int id,String type){
        User user=new User();
        user.setId(id);
        user.setType(type);
        userMapper.updateById(user);

    }

    @Override
    public void setUserAvatar(int id,String avatar){
        User user=new User();
        user.setId(id);
        user.setAvatar(avatar);
        userMapper.updateById(user);
    }

    @Override
    public boolean updateUser(UserInfo userInfo) {
        return userMapper.updateUserInfo(userInfo);
    }

    @Override
    public int addUserEnterprise(UserEnterprise userEnterprise) {
        userEnterprise.setFollowTime(LocalDateTime.now().toString());
        return userEnterpriseMapper.insert(userEnterprise);
    }

    @Override
    public int cancelUserEnterprise(int user_id, int ent_id) {
        QueryWrapper<UserEnterprise> eq = new QueryWrapper<UserEnterprise>()
                .eq("user_id", user_id)
                .eq("ent_id", ent_id);
        return userEnterpriseMapper.delete(eq);
    }

    @Override
    public UserEnterprise getUserEnterprise(int user_id, int ent_id) {
        QueryWrapper<UserEnterprise> userEnterpriseQueryWrapper = new QueryWrapper<>();
        userEnterpriseQueryWrapper.eq("user_id", user_id).eq("ent_id", ent_id);
        return userEnterpriseMapper.selectOne(userEnterpriseQueryWrapper);
    }

    @Override
    public boolean followEnterprise(int user_id, int ent_id) {
        QueryWrapper<UserEnterprise> userEnterpriseQueryWrapper = new QueryWrapper<>();
        userEnterpriseQueryWrapper.eq("user_id", user_id).eq("ent_id", ent_id);
        UserEnterprise userEnterprise = userEnterpriseMapper.selectOne(userEnterpriseQueryWrapper);
        if (userEnterprise == null) {
            UserEnterprise userEnterprise1 = new UserEnterprise();
            userEnterprise1.setUserId(user_id);
            userEnterprise1.setEntId(ent_id);
            userEnterprise1.setFollowTime(LocalDateTime.now().toString());
            int insert = userEnterpriseMapper.insert(userEnterprise1);
            return insert > 0;
        }
        userEnterpriseMapper.delete(userEnterpriseQueryWrapper);
        return true;
    }
}
